<template>
  <div class="login">
    <div class="back">
      <a class="fanhui" @click="$router.back()">
        <i class="iconfont icon-fanhui"> </i>
      </a>
    </div>
    <div class="main">
      <div class="top-wrap">
        <div class="top-title">{{ site.app_name }}</div>
        <div class="top-subtitle">
          <span class="child">{{ $t('邮箱注册') }}</span>
          <nuxt-link to="/register" class="child s2">{{ $t('手机号注册') }}</nuxt-link>
        </div>
      </div>
      <div class="form">
        <div class="input-group">
          <input v-model="email" type="text" class="input-default input-phone" :placeholder="$t('请输入邮箱')">
        </div>
        <div class="input-group">
          <input v-model="password" type="password" class="input-default input-pass" :placeholder="$t('请输入密码')"/>
        </div>
        <div class="input-group">
          <input v-model="repassword" type="password" class="input-default input-repass" :placeholder="$t('请再次输入密码')"/>
        </div>
        <div class="input-group">
          <input v-model="varcode" type="text" class="input-default input-sms-code" :placeholder="$t('请输入验证码')"/>
          <span v-if="count === 60" class="btn-sms" @click="sendCode">{{ $t('发送验证码') }}</span>
          <span v-else class="btn-sms">{{ count }}s</span>
        </div>
        <div class="input-group">
          <input v-model="code" type="text" class="input-default input-referral" :placeholder="$t('邀请码(选填)')"/>
        </div>
        <div class="form-footer">
          <div class="btn-submit" @click="sendForm">{{ $t('注册') }}</div>
        </div>
      </div>
      <div class="agreement">
        <span class="agreement-text">
          {{ $t('我已阅读并同意') }}<nuxt-link :to="`/user/about/about-iframe?title=${$t('用户协议')}&id=45`" class="agreement-link">《{{ site.app_name }} {{ $t('用户协议') }}》</nuxt-link>
        </span>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import { isEmail, isPassword } from '@/utils/validate'
import { AREA_CODE } from '@/constants/area-code'
let timerId
export default {
  data () {
    return {
      loading: false,
      codeList: AREA_CODE,
      count: 60,
      email: '',
      password: '',
      repassword: '',
      varcode: '',
      code: ''
    }
  },
  computed: {
    ...mapState({
      site: ({ initInfo }) => initInfo.site
    })
  },
  methods: {
    ...mapActions({
      sendEmail: 'auth/sendEmail',
      emailSign: 'auth/emailSign'
    }),
    resetCount () {
      this.count = 60
      clearInterval(timerId)
    },
    countdown () {
      this.resetCount()
      this.count--
      timerId = setInterval(() => {
        this.count--
        if (this.count === 0) {
          this.resetCount()
        }
      }, 1000)
    },
    sendCode () {
      if (this.loading) { return }
      do {
        if (!this.email && !isEmail(this.email)) {
          this.errmsg = this.$t('邮箱格式错误')
          break
        }
      } while (false)
      if (this.errmsg) {
        this.$toast.error(this.errmsg)
        this.errmsg = ''
        return
      }
      const payload = {
        type: 1,
        email: this.email
      }
      const succeed = () => {
        this.loading = false
        this.countdown()
      }
      const failed = (data) => {
        this.loading = false
        this.resetCount()
        this.$toast.error(data.message)
      }
      this.loading = true
      this.sendEmail(payload)
        .then(succeed)
        .catch(failed)
    },
    sendForm () {
      if (this.loading) { return }
      do {
        if (!this.email && !isEmail(this.email)) {
          this.errmsg = this.$t('邮箱格式错误')
          break
        }
        if (this.password === '') {
          this.errmsg = this.$t('密码不能为空')
          break
        }
        if (!isPassword(this.password)) {
          this.errmsg = this.$t('密码由6-20个数字、26个英文字母或者下划线组成')
          break
        }
        if (this.password !== this.repassword) {
          this.errmsg = this.$t('两次输入密码不一致')
          break
        }
        if (!this.varcode) {
          this.errmsg = this.$t('请输入验证码')
          break
        }
      } while (false)
      if (this.errmsg) {
        this.$toast.error(this.errmsg)
        this.errmsg = ''
        return
      }
      const payload = {
        code: this.code,
        email: this.email,
        varcode: this.varcode,
        password: this.password,
        repassword: this.repassword
      }
      const succeed = () => {
        this.loading = false
        this.$toast.success(this.$t('注册成功'))
        this.$router.replace({ path: '/login' })
      }
      const failed = (data) => {
        this.loading = false
        this.$toast.error(data.message)
      }
      this.loading = true
      this.emailSign(payload)
        .then(succeed)
        .catch(failed)
    }
  }
}
</script>
<style scoped>

.main {
  padding: 3% 5%;
}
.top-wrap {
  padding: 10px 0;
}
.top-title {
    width: 100%;
    font-size: 28px;
    color: #173e5d;
    line-height: 50px;
    font-weight: bold;
}
.top-subtitle {
    color: #999;
    line-height: 30px;
    font-size: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.top-subtitle .child {
  font: inherit;
  color: inherit
}
.top-subtitle .child.s2 {
  font-size: 12px;
}

.form {
  padding: 10px 0;
}
.form-footer {
  margin-top: 20px;
}
.input-group {
  width: 100%;
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgba(238, 238, 238, 0.8);
}
.input-default {
  height: 50px;
  line-height: 50px;
  padding-left: 4px;
  border: 1px solid transparent;
  caret-color:#007aff;
  font-size: 15px;
}
.input-areacode {
  width: 50px;
  height: 32px;
  line-height: 32px;
  text-align: center;
  color: #007aff;
  position: relative;
  font-size: 16px;
}
.native-phone-code-select {
  color: rgba(0,0,0,.0001);
  background-color: transparent;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.input-phone {
  flex: 1;
}
.input-pass {
  flex: 1;
}
.input-repass {
  flex: 1;
}
.input-sms-code {
  flex: 1;
}
.btn-sms {
  font-size: 14px;
  color: #007aff;
  padding: 4px;
  min-width: 80px;
  text-align: center;
  cursor: pointer;
}
.input-referal {
  flex: 1;
}
.btn-submit {
  width: 100%;
  height: 40px;
  line-height: 40px;
  background-color: #007aff;
  color: white;
  font-size: 18px;
  text-align: center;
  border-radius: 4px;
  cursor: pointer;
  letter-spacing: 4px;
}
.btn-submit.disabled {
  opacity: 0.8;
}

.agreement {
  text-align: center;
  padding: 10px 0;
}

.agreement-text {
  font-size: 14px;
}

.agreement-link {
  font-size: 14px;
  color: #007aff;
  text-decoration: none;
}
</style>
